package com.rcadmin.busi.logread.dto;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.google.common.collect.Lists;

/**
 * 文件节点
 * 
 * @author tanson lam
 * @create 2016年9月7日
 */
public class FileNode {
	/**
	 * 文件名称
	 */
	private String name;
	/**
	 * 文件路径
	 */
	private String path;
	/**
	 * 包含子目录
	 */
	private List<FileNode> children;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public List<FileNode> getChildren() {
		return children;
	}

	public void setChildren(List<FileNode> children) {
		this.children = children;
	}

	public static List<FileNode> getChildNode(File rootFile) throws IOException {
		List<FileNode> innerNodes = Lists.newArrayList();
		if (!rootFile.isDirectory())
			throw new IOException("rootFile is not a directory."
					+ rootFile.getAbsolutePath());
		File[] files = rootFile.listFiles();
		for (File file : files) {
			FileNode fileNode = new FileNode();
			fileNode.name = file.getName();
			fileNode.path = file.getAbsolutePath();
			if (file.isDirectory()) {
				fileNode.children = getChildNode(file);
			}
			innerNodes.add(fileNode);
		}
		return innerNodes;
	}

	@Override
	public String toString() {
		return "FileNode [name=" + name + ", path=" + path + ", children="
				+ (children != null ? Arrays.toString(children.toArray()) : null)
				+ "]";
	}

	public static void main(String[] args) throws IOException {
		List<FileNode> d = getChildNode(new File("D:/downloads"));
		System.out.println(Arrays.toString(d.toArray()));
	}
}
